using SmartCommunity.Services.Workorder;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.Controllers.Workorder
{
    /// <summary>
    /// API接口：工单处理过程记录
    /// </summary>
    [ApiController]
    [Route("api/workorder/[controller]")]
    public class WorkProcessController : BaseController
    {
        #region 构造函数
        private readonly WorkProcessService workProcessService;
        public WorkProcessController(WorkProcessService workProcessService)
        {
            this.workProcessService = workProcessService;
        }
        #endregion

        #region 查询导出 
        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<WorkProcessDto>> GetList([FromQuery] WorkProcessQuery query, [FromQuery] Pager pager)
        {
            return await workProcessService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<WorkProcessDto> GetOne(Guid id)
        {
           return await workProcessService.FindWorkProcessById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] WorkProcessQuery query)
        {
            var pageData = await workProcessService.FindPagedList(query, Pager.Unpaged, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "工单id", "处理内容描述");
            int rowIndex = 1;
            foreach (var data in pageData.DataList)
            {
                exporter.AddDataRow(rowIndex++, 0, data.OrderId, data.Content );
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"工单处理过程记录导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增工单处理过程记录
        /// </summary>
        [HttpPost]
        [Log("新增工单处理过程记录", OperateType.Create, false, true)]
        public async Task<ActionResult<WorkProcessDto>> Create([FromBody] WorkProcessDto dto)
        {
            var workProcess = await workProcessService.CreateWorkProcess(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = workProcess.ProcessId }, workProcess);
        }

        /// <summary>
        /// 修改工单处理过程记录
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改工单处理过程记录", OperateType.Update)]
        public async Task<ActionResult> Update(Guid id, [FromBody] WorkProcessDto dto)
        {
            if (id != dto.ProcessId)
            {
                return ErrorMessage("要更新的数据和ID不匹配");
            }
            await workProcessService.UpdateWorkProcess(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除工单处理过程记录
        /// </summary>
        [HttpDelete]
        [Log("删除工单处理过程记录", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] Guid[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await workProcessService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
